import router from './router'
import store from './store'
import nprogress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach((to, from, next) => {
  // 开启进度条
  nprogress.start()
  if (store.getters.token) {
    // 有token
    if (to.path === '/login') {
      // 即将要去登录页
      next('/') // 跳转到首页
      nprogress.done() // 关闭进度条
      // 被拦截到其他页面时不会触发后置守卫
    } else {
      next()
    }
  } else {
    // 没有token
    if (whiteList.includes(to.path)) {
      next() // 正常放行
    } else {
      next('/login')
      nprogress.done() // 关闭进度条
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  nprogress.done()
})
